﻿//-----------------------------------------------------------------------
// <copyright file="GoatObject.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace GoatGen.Object
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Object that is build from a Goat file.
    /// </summary>
    public class GoatObject
    {
        #region Fields - Private

        /// <summary>
        /// Field name dictionary used to prevent duplicates.
        /// </summary>
        private IDictionary<string, GoatField> fieldNameDict = new Dictionary<string, GoatField>();

        /// <summary>
        /// Fields in the object.
        /// </summary>
        private IList<GoatField> fields = new List<GoatField>();

        #endregion

        #region Constructor

        /// <summary>
        /// Create the object and give it the passed name.
        /// </summary>
        /// <param name="name">Name of the object.</param>
        public GoatObject(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name", "Name of the object must be non-null.");
            }

            this.Name = name;
        }

        #endregion

        #region Properties - Public

        /// <summary>
        /// Gets the name of the object.
        /// </summary>
        public string Name
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the field that make up the object.
        /// </summary>
        public IEnumerable<GoatField> Fields
        {
            get
            {
                foreach (GoatField field in this.fields)
                {
                    if (field.Name == GoatFieldConstants.ConfigFieldName)
                    {
                        continue;
                    }

                    yield return field;
                }
            }
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Get a field given the passed name.
        /// </summary>
        /// <param name="name">Name of the field to get.</param>
        /// <returns>Returns the field or null if the field is not found.</returns>
        public GoatField GetField(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name", "Name must be non-null.");
            }

            GoatField field;

            this.fieldNameDict.TryGetValue(name, out field);

            return field;
        }

        /// <summary>
        /// Add a field to the object. If the field name is present already,
        /// a GoatException will be thrown.
        /// </summary>
        /// <param name="field">Field to add.</param>
        public void AddField(GoatField field)
        {
            if (field == null)
            {
                throw new ArgumentNullException("Field must be non-null.");
            }

            if (this.fieldNameDict.ContainsKey(field.Name))
            {
                throw new GoatException("Duplicate field name found: " + field.Name);
            }

            this.fields.Add(field);
            this.fieldNameDict.Add(field.Name, field);
        }

        #endregion
    }
}
